Imports DAL
Imports System
Imports MyClasses
Imports System.Configuration

Public Class frmOpeningStock
    Dim objBillItem() As CPurchaseItem
    Dim CurrentRow As Integer = 1
    Dim flag As Byte
    Dim dtDItems As DataTable
    Dim flagVendor As Byte

    Private Sub frmOpeningStock_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Opening Stock")
        e.Cancel = True
    End Sub

    Private Sub frmOpeningStock_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Opening Stock")
        End If
    End Sub

    Private Sub frmOpeningStock_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Call BCommon.SetFormStandards(Me)
            Call Grid_Settings()
            Call DisplayData()
            flagVendor = 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmOpeningStock_Load")
        End Try
    End Sub

    Private Sub DisplayData()
        Try
            BCommon.SetComboValues(cmbVendor, BVendor.GetVendor(), "VendorID", "VendorName")
            cfgItems.Cols(1).Editor = Nothing
            ChkAll.Checked = False
            ChkAll.Enabled = False
            flagVendor = 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
        End Try
    End Sub

    Private Sub Grid_Settings()
        Try
            cfgItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
            Call SetGridCaptions()
            Call SetColumnWidths()
            ''Common Settings
            With cfgItems
                .Cols(1).Editor = cmDItem
                'EDITABLE/NON-EDITABLE COLUMNS
                .Cols(0).Visible = False        'Item ID
                .Cols(0).AllowEditing = False  'ITEM ID
                .Cols(1).AllowEditing = True   'ITEM NAME
                .Cols(2).AllowEditing = True   'BATCH
                .Cols(3).AllowEditing = True   'MFG DATE
                .Cols(4).AllowEditing = True   'EXPIRY
                .Cols(5).AllowEditing = True   'QUANTITY
                .Cols(6).AllowEditing = True   'MRP
                .Cols(7).AllowEditing = True   'BILL RATE
                .Cols(8).AllowEditing = True   'SALE RATE
                .Cols(0).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.LeftCenter
                'DISPLAY FORMAT   
                .Cols(3).DataType = System.Type.GetType("System.DateTime")
                .Cols(4).DataType = System.Type.GetType("System.DateTime")
                .Cols(5).DataType = System.Type.GetType("System.Int32")
                .Cols(6).DataType = System.Type.GetType("System.Decimal")
                .Cols(7).DataType = System.Type.GetType("System.Decimal")
                .Cols(8).DataType = System.Type.GetType("System.Decimal")
                .Cols(3).Format = ConfigurationManager.AppSettings("DateFormat").ToString()
                .Cols(4).Format = ConfigurationManager.AppSettings("DateFormat").ToString()
                .Cols(6).Format = "0.00"  '"M.R.P."
                .Cols(7).Format = "0.00"  '"BILL RATE"
                .Cols(8).Format = "0.00"  '"SALE RATE"
                .Cols(1).Editor = cmDItem
                .Rows.Count = 17
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Grid_Settings")
        End Try
    End Sub

    Private Sub SetGridCaptions()
        Try
            With cfgItems
                .Cols.Count = 9 '7
                .Row = 1
                .Rows.Count = 17
                .Rows.Fixed = 1
                'CAPTIONS
                .Cols(0).Caption = "ID"
                .Cols(1).Caption = "Item Name"
                .Cols(2).Caption = "Batch No."
                .Cols(3).Caption = "MFG Date"
                .Cols(4).Caption = "Expiry Date "
                .Cols(5).Caption = "Quantity"
                .Cols(6).Caption = "M.R.P."
                .Cols(7).Caption = "Rate"
                .Cols(8).Caption = "Sale Rate"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGridCaptions")
        End Try
    End Sub

    Private Sub SetColumnWidths()
        Dim intGridWidth As Integer = cfgItems.Width - 5
        Try
            With cfgItems()
                .Cols(0).Width = 0           '"ITEM ID"
                .Cols(1).Width = intGridWidth / 5           '"ITEM NAME"
                .Cols(2).Width = intGridWidth / 10            '"BATCH"
                .Cols(3).Width = intGridWidth / 8           '"MFG DATE"
                .Cols(4).Width = intGridWidth / 8          '"EXPIRY DATE"
                .Cols(5).Width = intGridWidth / 10            '"ITEM QUANTITY"
                .Cols(6).Width = intGridWidth / 10            '"MRP"
                .Cols(7).Width = intGridWidth / 10           '"BILL RATE"
                .Cols(8).Width = intGridWidth / 10          '"SALE RATE"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetColumnWidths")
        End Try
    End Sub

    Private Sub ClearData()
        Try
            flagVendor = 0
            cmbVendor.SelectedIndex = -1
            cmDItem.SelectedIndex = -1
            ChkAll.Checked = False
            ChkAll.Enabled = False
            txtVendorID.Text = ""
            CurrentRow = 1
            Call Grid_Settings()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If ValidateData() = 0 Then
                Exit Sub
            Else
                Call SaveData()
                Call ClearData()
                Call DisplayData()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Sub

    Private Sub SaveData()
        Try
            Dim intResult As Integer
            Call SetData()
            intResult = DPurchaseInvoice.SaveOpeningStock(objBillItem, cmbVendor.SelectedValue)
            If intResult > 0 Then flag = 1 : BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
        End Try
    End Sub

    Private Sub SetData()
        Dim intCount As Integer
        Dim dtbVItems As DataTable
        Try
            Dim intItems As Integer
            For intCount = 1 To cfgItems.Rows.Count - 2
                If Not IsNothing(cfgItems.Item(intCount, 0)) Then
                    If Trim(cfgItems.Item(intCount, 0)) <> "" OrElse Trim(cfgItems.Item(intCount, 0)) <> "0" Then
                        intItems = intItems + 1
                    End If
                End If
            Next
            ReDim objBillItem(intItems - 1)
            For intCount = 0 To intItems - 1
                objBillItem(intCount) = New CPurchaseItem
                With cfgItems
                    objBillItem(intCount).ExpiryDate = BCommon.GetDatewithLastDay(.Item(intCount + 1, 4))
                    objBillItem(intCount).ItemID = .Item(intCount + 1, 0)
                    objBillItem(intCount).BatchNo = .Item(intCount + 1, 2)
                    objBillItem(intCount).ManufactureDate = .Item(intCount + 1, 3)
                    objBillItem(intCount).Quantity = .Item(intCount + 1, 5)
                    objBillItem(intCount).StockQuantity = .Item(intCount + 1, 5)
                    objBillItem(intCount).MRP = .Item(intCount + 1, 6)
                    objBillItem(intCount).BillRate = .Item(intCount + 1, 7)
                    objBillItem(intCount).PurchasePrice = .Item(intCount + 1, 7)
                    objBillItem(intCount).Net_Rate = .Item(intCount + 1, 7)
                    objBillItem(intCount).SalePrice1 = .Item(intCount + 1, 8)
                    'Items which r not till supplied by by that vendor but now started suppling new item
                    dtbVItems = DItem.GetValidVendor(cmbVendor.SelectedValue, .Item(intCount + 1, 0))
                    If dtbVItems.Rows.Count <= 0 Then
                        Dim intManufacurer As Integer = DManufacturer.GetManufacturer(.Item(intCount + 1, 0))
                        Dim intResult As Integer = DItem.SaveItemsMV(intManufacurer, cmbVendor.SelectedValue, .Item(intCount + 1, 0))
                        If intResult = 0 Then BCommon.ShowMsgError(4) : Exit Sub
                    End If
                End With
            Next
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    'Divya_ITS@20071230 : Added form closing event 
    Private Sub frmOpeningStock_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdated() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateData() Then
                                Call SaveData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub

    Private Function isUpdated() As Boolean
        Try
            If checkTag() Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdated")
        End Try
    End Function

    Private Function checkTag() As Boolean
        Try
            If cmbVendor.Tag <> cmbVendor.SelectedValue Or txtVendorID.Tag <> txtVendorID.Text Then
                Return True
            Else : Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "checkTag")
        End Try

    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Call ClearData()
        Call DisplayData()
    End Sub

    Private Sub cfgItems_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItems.AfterEdit
        Dim intCount As Integer, intResult As Integer
        Dim dtbBatchDetail As DataTable
        Try
            With cfgItems
                Select Case .Col
                    Case 1
                        If cmDItem.SelectedIndex <> -1 Then
                            .Item(e.Row, 0) = cmDItem.SelectedValue
                            CurrentRow = .Row
                        End If
                    Case 2
                        If Not IsNothing(.Item(.Row, 2)) Or .Item(.Row, 2) <> "" Then

                            intResult = DPurchaseInvoice.IsStockAvail(.Item(.Row, 0), .Item(.Row, 2))
                            If intResult > 0 Then
                                'MessageBox.Show(BCommon.GetMessage(10268), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
                                BCommon.ShowMsgError(10268)
                                .Item(.Row, 2) = ""
                                .Col = 2
                                Exit Sub
                            End If
                            dtbBatchDetail = DPurchaseInvoice.BatchDetail(.Item(.Row, 0), .Item(.Row, 2))
                            If dtbBatchDetail.Rows.Count > 0 Then
                                .Item(e.Row, 3) = dtbBatchDetail.Rows(0).Item("MfgDate")
                                .Cols(3).AllowEditing = False
                                .Item(e.Row, 4) = dtbBatchDetail.Rows(0).Item("ExpDate")
                                .Cols(4).AllowEditing = False
                                .Item(e.Row, 6) = dtbBatchDetail.Rows(0).Item("MRP")
                            Else
                                .Cols(3).AllowEditing = True
                                .Item(e.Row, 3) = Nothing
                                .Cols(4).AllowEditing = True
                                .Item(e.Row, 4) = Nothing
                                .Item(e.Row, 6) = Nothing
                            End If
                        End If
                    Case 3
                        If .Item(.Row, 3) > Date.Today Then
                            BCommon.ShowMsgError(10119)
                            '.Item(.Row, 3) = Format(Date.Today, "MMM/yy")
                            .Item(.Row, 3) = Now.Date.AddDays(-1)
                        End If
                    Case 4
                        If Date.Today > .Item(.Row, 4) Then
                            BCommon.ShowMsgError(10113)
                            '.Item(.Row, 4) = Format(Date.Today, "MMM/yy")
                            .Item(.Row, 4) = BCommon.GetDatewithLastDay(Now.Date)
                            e.Cancel = True
                        End If
                End Select
                If .Row > 0 Then
                    For intCount = 1 To .Rows.Count - 2
                        If .Item(intCount + 1, 0) = .Item(intCount, 0) Then
                            If Not IsNothing(.Item(intCount + 1, 2)) Then
                                If .Item(intCount + 1, 2) = .Item(intCount, 2) Then
                                    BCommon.ShowMsgError(10269)
                                    .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                                    .Rows.Remove(.Row)
                                    .Col = 1
                                    Exit Sub
                                End If
                                Exit Sub
                            Else
                                Exit Sub
                            End If
                        End If
                    Next
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItems_AfterEdit")
        End Try
    End Sub

    Private Function ValidateData() As Byte
        Dim intCount As Integer
        Try
            If cmbVendor.SelectedValue <= 0 Then
                'MessageBox.Show(BCommon.GetMessage(10270), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
                BCommon.ShowMsgError(10270)
                cmbVendor.Focus()
                Return 0
            End If

            With cfgItems
                If cfgItems.Item(1, 0) = 0 Then
                    BCommon.ShowMsgError(10120) : Return 0
                    Return 0
                End If
            End With

            With cfgItems
                For intCount = 1 To cfgItems.Rows.Count - 1
                    If .Item(intCount, 0) = 0 Then Return 1
                    'BATCH
                    If IsNothing(.Item(intCount, 2)) Or .Item(intCount, 2) = "" Then
                        BCommon.ShowMsgError(10112)
                        .Col = 2
                        Return 0
                    End If
                    'MFD DATE
                    If IsNothing(.Item(intCount, 3)) Then
                        'MessageBox.Show(BCommon.GetMessage(10271), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        BCommon.ShowMsgError(10271)
                        .Col = 3
                        Return 0
                    End If
                    'EXPIRY DATE
                    If IsNothing(.Item(intCount, 4)) Then
                        'MessageBox.Show(BCommon.GetMessage(10272), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        BCommon.ShowMsgError(10272)
                        .Col = 4
                        Return 0
                    End If
                    'QUANTITY
                    If IsNothing(.Item(intCount, 5)) Then
                        BCommon.ShowMsgError(10114)
                        .Col = 5
                        Return 0
                    End If

                    'MRP
                    If IsNothing(.Item(intCount, 6)) Or .Item(intCount, 6) = 0 Then
                        BCommon.ShowMsgError(10115)
                        .Col = 6
                        Return 0
                    End If
                    'BILL RATE
                    If IsNothing(.Item(intCount, 7)) Or .Item(intCount, 7) = 0 Then
                        BCommon.ShowMsgError(10116)
                        .Item(intCount, 7) = .Item(intCount, 6)
                        .Col = 7
                        Return 0
                    End If
                    'SALE RATE
                    If IsNothing(.Item(intCount, 8)) Or .Item(intCount, 8) = 0 Then
                        'MessageBox.Show(BCommon.GetMessage(10273), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        BCommon.ShowMsgError(10273)
                        .Col = 8
                        Return 0
                    End If
                Next
            End With
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
        End Try
    End Function

    Private Sub cfgItems_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItems.BeforeEdit
        If e.Col = 1 Then
            If IsNothing(cfgItems.Item(e.Row - 1, 1)) Then e.Cancel = True
        End If
    End Sub

    Private Sub cfgItems_EnterCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgItems.EnterCell
        Try
            With cfgItems
                If .Row <> CurrentRow Then  'Checks is previous item details filled completely
                    Dim intBlankColumn As Integer
                    intBlankColumn = isValidRow()
                    If intBlankColumn > 0 Then
                        If .Col = 1 Or .Col = 2 Then
                            SetActiveCell(CurrentRow, intBlankColumn)
                            Exit Sub
                        Else
                            SetActiveCell(CurrentRow, intBlankColumn)
                            Exit Sub
                        End If
                    End If
                End If
                If flag = 0 Then
                    Select Case .Col
                        Case 1, 2, 3, 4, 5, 6, 7, 8
                            If cmbVendor.SelectedValue <= 0 Then
                                .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                                'MessageBox.Show(BCommon.GetMessage(10274), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                BCommon.ShowMsgError(10274)
                                cmbVendor.Focus()
                                Exit Sub
                            End If
                            If .Col <> 1 Then
                                If .Item(.Row, 0) = 0 Then
                                    BCommon.ShowMsgError(10120)
                                    .Col = 1
                                End If
                            End If
                    End Select
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItems_EnterCell")
        End Try
    End Sub

    Private Function isValidRow() As Integer
        Try
            With cfgItems
                If IsNothing(.Item(CurrentRow, 0)) OrElse Trim(.Item(CurrentRow, 0)) = "" Then Return ShowColError(1)
                'Is Batch Enterd
                If IsNothing(.Item(CurrentRow, 2)) OrElse Trim(.Item(CurrentRow, 2)) = "" Then Return ShowColError(2)
                'Is Manufacturing Date Entered Properly
                If IsNothing(.Item(CurrentRow, 3)) OrElse Trim(.Item(CurrentRow, 3)) = "" Then .Col = 3 : Return ShowColError(3)
                'Is Expiry Entered Properly
                If IsNothing(.Item(CurrentRow, 4)) OrElse Trim(.Item(CurrentRow, 4)) = "" Then .Col = 4 : Return ShowColError(4)
                'IS Quantity Enterd 
                If IsNothing(.Item(CurrentRow, 5)) OrElse Trim(.Item(CurrentRow, 5)) <= 0 Then Return ShowColError(5)
                'IS MRP Entered 
                If IsNothing(.Item(CurrentRow, 6)) OrElse Trim(.Item(CurrentRow, 6)) <= 0 OrElse Trim(.Item(CurrentRow, 6)) = "" Then Return ShowColError(6)
                'IS Bill Rate Entered 
                If IsNothing(.Item(CurrentRow, 7)) OrElse Trim(.Item(CurrentRow, 7)) <= 0 Then Return ShowColError(7) 'Return 0
                'IS Sale Rate Entered 
                If IsNothing(.Item(CurrentRow, 8)) OrElse Trim(.Item(CurrentRow, 8)) <= 0 Then Return 0
                Return 0
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isValidRow")
            Return 0
        End Try
    End Function

    Private Function ShowColError(ByVal intCol As Integer) As Byte
        Try
            Select Case intCol
                Case 0 'Item
                    BCommon.ShowMsgError(10111)
                    intCol = 1
                Case 2 : BCommon.ShowMsgError(10112) '"BATCH"
                    'Case 3 : MessageBox.Show(BCommon.GetMessage(10273), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information) '"MANUFACTURED"
                Case 3 : BCommon.ShowMsgError(10273)
                    'Case 4 : MessageBox.Show(BCommon.GetMessage(10274), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information) 'BCommon.ShowMsgError(10113) '"EXPIRY"
                Case 4 : BCommon.ShowMsgError(10274)
                Case 5 : BCommon.ShowMsgError(10114) '"QUANTITY"
                Case 6 : BCommon.ShowMsgError(10115) '"M.R.P."
                Case 7 : BCommon.ShowMsgError(10116) '"BILL RATE"
                Case 8 : BCommon.ShowMsgError(10117) '"SALE RATE"
                Case Else : intCol = 0
            End Select
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowColError")
        Finally
            ShowColError = intCol
        End Try
    End Function

    Private Sub SetActiveCell(ByVal intRow As Integer, ByVal intCol As Integer)
        Try
            With cfgItems
                .Row = intRow
                .RowSel = intRow
                .Col = intCol
                .ColSel = intCol
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetActiveCell")
        End Try
    End Sub

    Private Sub cfgItems_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles cfgItems.ValidateEdit
        Try
            With cfgItems
                Select Case .Col
                    Case 5  'Quantity
                        If Trim(cfgItems.Editor.Text) <= "0" Or cfgItems.Editor.Text = "" Then
                            BCommon.ShowMsgError(10114)
                            e.Cancel = True
                        End If

                    Case 7   'BILL RATE 
                        If Val(cfgItems.Editor.Text) > .Item(e.Row, 6) Then
                            'cfgItems.Editor.Text
                            'MessageBox.Show(BCommon.GetMessage(10275), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
                            BCommon.ShowMsgError(10275)
                            e.Cancel = True         'Put cursor on same column
                        End If
                    Case 8   'SALE RATE 
                        If Val(cfgItems.Editor.Text) > .Item(e.Row, 6) Then
                            'MessageBox.Show(BCommon.GetMessage(10276), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
                            BCommon.ShowMsgError(10276)
                            e.Cancel = True         'Put cursor on same column
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItems_ValidateEdit")
        End Try
    End Sub

    Private Sub ChkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkAll.CheckedChanged
        Call DisplayItems()
    End Sub

    Private Sub DisplayItems()
        Try
            If ChkAll.Checked = True And cmbVendor.SelectedIndex <= 0 Then 'If vendor is not selected
                dtDItems = DItem.GetItemsPI()
                dtDItems.PrimaryKey = New DataColumn() {dtDItems.Columns(0)} 'Item ID
            ElseIf ChkAll.Checked = True And cmbVendor.SelectedIndex > 0 Then 'If vendor & Check box is selected
                dtDItems = DItem.GetItemsPI()
            Else
                dtDItems = DItem.GetvendorItems(cmbVendor.SelectedValue) 'DItem.GetItems(StrItems)
            End If
            cmDItem.DataSource = dtDItems
            cmDItem.ValueMember = "ItemID"
            cmDItem.DisplayMember = "Name"
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayItems")
        End Try
    End Sub

    Private Sub cmbVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVendor.KeyDown
        If e.KeyCode = Keys.Enter Then
            If sender.selectedindex = -1 Then
                cmbVendor.Focus()
                sender.text = ""
            Else
                ChkAll.Focus()
            End If
        End If
    End Sub

    Private Sub cmbVendor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.LostFocus
        If sender.selectedindex = -1 Then
            cmbVendor.Focus()
            sender.text = ""
        End If
    End Sub

    Private Sub cmbVendor_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.SelectedValueChanged
        If flagVendor = 0 Then Exit Sub
        Try
            If cmbVendor.SelectedValue > 0 Then
                If Not IsNothing(cfgItems.Item(1, 0)) Then
                    Call Grid_Settings()
                    cfgItems.Col = 1
                End If
                ChkAll.Enabled = True      '**
                txtVendorID.Text = ""
                txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmbVendor.SelectedValue.ToString)
                cfgItems.Cols(1).Editor = cmDItem
            Else
                ChkAll.Enabled = False
                cfgItems.Cols(1).Editor = Nothing
                Call DisplayData()
            End If
            Call DisplayItems()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendor_SelectedValueChanged")
        End Try
    End Sub

    Private Sub txtVendorID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVendorID.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtVendorID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVendorID.LostFocus
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.txtVendorID.Text <> "" Then
                dsVendorId = BVendor.GetVendorId(txtVendorID.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmbVendor.SelectedValue = intVendId
                    cmbVendor.Focus()
                Else
                    cmbVendor.SelectedValue = 0
                    'MessageBox.Show(BCommon.GetMessage(10277), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    BCommon.ShowMsgError(10277)
                    txtVendorID.Text = ""
                    Me.txtVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtVendorID_LostFocus")
        End Try
    End Sub

    Private Sub cmDItem_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmDItem.KeyDown
        If e.KeyCode = Keys.Enter Then
            If sender.selectedindex = -1 Then sender.text = ""
        End If
    End Sub

    Private Sub cmDItem_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmDItem.Validating
        If sender.selectedindex = -1 Then sender.text = ""
    End Sub

    Private Sub ChkAll_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ChkAll.KeyDown
        cfgItems.Col = 1
    End Sub
End Class